import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { DemandTeamRequestEntity } from '../../entity/request';
import { DemandRequestService } from '../../service/request';
import { ProviderUpdateDemandStatusDTO } from '../../dto/request';
import { Context } from '@midwayjs/koa';

@CoolController({
    api: ['info'],
    entity: DemandTeamRequestEntity,
    service: DemandRequestService,
})
export class ProviderDemandRequestController extends BaseController {
    @Inject()
    ctx: Context;

    @Inject()
    demandRequestService: DemandRequestService;

    /**
     * 团队需求分页
     */
    @Post('/pageByTeam', { summary: '获取团队需求列表' })
    async pageByTeam(@Body() query) {
        const teamId = this.ctx.team?.id;
        if (!teamId) {
            throw new CoolCommException('团队信息不存在');
        }
        return this.ok(await this.demandRequestService.pageByTeam(teamId, query ?? {}));
    }

    /**
     * 根据团队ID获取需求信息
     */
    @Post('/infoByTeam', { summary: '获取团队需求信息' })
    async infoByTeam(@Body() query) {
        const teamId = this.ctx.team?.id;
        if (!teamId) {
            throw new CoolCommException('团队信息不存在');
        }
        return this.ok(await this.demandRequestService.infoByTeam(teamId, Number(query.id)));
    }

    /**
     * 更新需求状态
     */
    @Post('/updateStatus', { summary: '更新需求状态' })
    async updateStatus(@Body() dto: ProviderUpdateDemandStatusDTO) {
        const teamId = this.ctx.team?.id;
        if (!teamId) {
            throw new CoolCommException('团队信息不存在');
        }
        await this.demandRequestService.updateStatus(teamId, this.ctx.provider.id, dto);
        return this.ok();
    }
}


